<?php

class ModuleResultOfRegActiveConfirm extends CmsModule {
    //set the public head
    private function setHeadString($condition=null,$sort_by=null,$rand_code) {
        $date_list = null;
        $current_time = date('Y-m-d H:i:s');

        if (!empty($condition) && is_array($condition)) {
            //date time.
            $date_list = '<strong>By '.ucwords($condition['date_by']).': </strong>&nbsp; '.$condition['date_time'];
        }

        $condition_list = '<br/>'.
                          '<div>'.$date_list.'</div>';

       //control tag.                         
        $export_tag =  'Export to '.AC::exportHtmlControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_REG_ACTIVE_CONFRIM)
                                   .AC::exportPdfControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_REG_ACTIVE_CONFRIM)
                                   .AC::exportCvsControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_REG_ACTIVE_CONFRIM);

        $head_string = '<div class="hd">'.
                       '    <div class="utility">'.
                       '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$export_tag.
                       '    </div>'.
                       '    <hr>'.
                       '    <h1>'.$this->title.'</h1>'.
                       '    <div class="ui-helper-clearfix">'.
                       '        <div class="time">'.$current_time.'</div>'.
                       '        <ul>'.$condition_list.'</ul>'.
                       '    </div>'.
                       '</div>';

        $head_string_exprot = '<div class="hd">'.
                                '    <div class="utility">'.
                                '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
                                '    </div>'.
                                '    <hr>'.
                                '    <h1>'.$this->title.'</h1>'.
                                '    <div class="ui-helper-clearfix">'.
                                '        <div class="time">'.$current_time.'</div>'.
                                '        <ul>'.$condition_list.'</ul>'.
                                '    </div>'.
                                '</div>';

        return array('view'=>$head_string,'export'=>$head_string_exprot);
    }    

    //out put the body string.
    private function getOutPutBody($head_string,$body_string) {
        $output ='<div id="result" class="reg_active_confirm_result" >'.$head_string.
                 '  <div class="bd">'.
                 '  <br />'.$body_string.
                 '  </div>'.
                 '</div>';
        return $output;
    }
    
    //get the page search filter condition.
    private function getPageCondition($params) {
        $date_by = (!empty($params['dateby'])) ? $params['dateby'] : 'month';
        
        switch($date_by) {
            case 'month':
                $date_time = $params['year'];
                break;
            case 'date':
                $date_time = $params['year'].'-'.$params['month'];
                break;
            case 'hour':
                $date_time = $params['year'].'-'.$params['month'].'-'.$params['day'];
                break;
        }

        return array('condition'=>array('date_by'=>$date_by,'date_time'=>$date_time));
    }
    
    //set the header string.
    private function setHeaderString($date_by) {
        $date_by_str = ($date_by=='month') ? '月份' : (($date_by=='date') ? '日期' : (($date_by=='hour') ? '小時' : null));
        $title_show = AC::$title_of_membership_reg_active_confirm;
        $body = '<tr class="weight" bgcolor="#CCC">'.
                '   <td>'.$date_by_str.'</td>';
        foreach($title_show as $item) {
            $body .= '<td>'.$item.'</td>';
        }
        $body .= '</tr>';
        return $body;
    }

    //set the body string.
    private function setBodyString($date_time,$date_by,$data) {
        $body_list = null;
        $total_type_arr = array('register'=>0,'confirm'=>0,'active'=>0,'expired_unconfirm'=>0,'expired_confirm'=>0);
        switch($date_by) {
            case 'month':
                $start_num = 1;
                $end_num = 12;
                $unit = 'n月';
                break;
            case 'date':
                $start_num = 1;
                $current_month = date('n',strtotime($date_time));
                $is_leap_year = date('L',strtotime($date_time));
                $end_num = ($current_month==2 && $is_leap_year) ? 28 : (($current_month==2 && !$is_leap_year) ? 29 : (in_array($current_month,array(4,6,9,11)) ? 30 : 31));
                $unit = 'n月j日';
                break;
            case 'hour':
                $start_num = 0;
                $end_num = 23;
                $unit = '時';
                break;
        }
        
        //set data list.
        for ($i=$start_num;$i<=$end_num;$i++) {
            $li = ($i<10) ? '0'.$i : $i;
            $default_format = ($date_by!='hour') ? $date_time.'-'.$li : $date_time.' '.$li;
            $register_num = (isset($data['register'][$default_format])) ? ($data['register'][$default_format]) : 0;
            $confirm_num =  (isset($data['confirm'][$default_format])) ? ($data['confirm'][$default_format]) : 0;
            $active_num = (isset($data['active'][$default_format])) ? ($data['active'][$default_format]) : 0;
            $expired_unconfirm_num = (isset($data['expired'][$default_format]) && isset($data['expired_confirm'][$default_format])) ? ($data['expired'][$default_format]-$data['expired_confirm'][$default_format]) : 0;
            $expired_confirm_num = (isset($data['expired_confirm'][$default_format])) ? ($data['expired_confirm'][$default_format]) : 0; 
            $body_list .=   '<tr>'.
                            '   <td>'.(($date_by!='hour') ? date($unit,strtotime($default_format)) : $li.$unit).'</td>'.
                            '   <td>'.number_format($register_num).'</td>'.//register
                            '   <td>'.number_format($confirm_num).'</td>'.//confirm
                            '   <td>'.number_format($active_num).'</td>'.//active
                            '   <td>'.number_format($expired_unconfirm_num).'</td>'.//expire un-confirm
                            '   <td>'.number_format($expired_confirm_num).'</td>'.//expire confirm
                            '</tr>';


            $total_type_arr['register'] +=  $register_num;
            $total_type_arr['confirm'] +=  $confirm_num;
            $total_type_arr['active'] +=  $active_num;
            $total_type_arr['expired_unconfirm'] +=  $expired_unconfirm_num;
            $total_type_arr['expired_confirm'] +=  $expired_confirm_num;
        }
        
        //set footer.
        $body_list .= '<tr>'.
                      ' <td><b>Total</b></td>'.
                      ' <td><b>'.number_format($total_type_arr['register']).'</b></td>'.
                      ' <td><b>'.number_format($total_type_arr['confirm']).'</b></td>'.
                      ' <td><b>'.number_format($total_type_arr['active']).'</b></td>'.
                      ' <td><b>'.number_format($total_type_arr['expired_unconfirm']).'</b></td>'.
                      ' <td><b>'.number_format($total_type_arr['expired_confirm']).'</b></td>'.
                      '</tr>'.
                      '<tr>'.
                      ' <td><b>Average</b></td>'.
                      ' <td><b>'.round($total_type_arr['register']/$end_num,2).'</b></td>'.
                      ' <td><b>'.round($total_type_arr['confirm']/$end_num,2).'</b></td>'.
                      ' <td><b>'.round($total_type_arr['active']/$end_num,2).'</b></td>'.
                      ' <td><b>'.round($total_type_arr['expired_unconfirm']/$end_num,2).'</b></td>'.
                      ' <td><b>'.round($total_type_arr['expired_confirm']/$end_num,2).'</b></td>'.
                      '</tr>';
        return $body_list;
    }

    //set image char string.
    private function setBodyCharString($date_time,$date_by,$data) {
        $captures = json_encode(AC::$title_of_membership_reg_active_confirm);

        $labels = array();
        $show_data = array();
        switch($date_by) {
            case 'month':
                $start_num = 1;
                $end_num = 12;
                $unit = '月';
                break;
            case 'date':
                $start_num = 1;
                $current_month = date('n',strtotime($date_time));
                $is_leap_year = date('L',strtotime($date_time));
                $end_num = ($current_month==2 && $is_leap_year) ? 28 : (($current_month==2 && !$is_leap_year) ? 29 : (in_array($current_month,array(4,6,9,11)) ? 30 : 31));
                $unit = '日';
                break;
            case 'hour':
                $start_num = 0;
                $end_num = 23;
                $unit = '時';
                break;
        }
        $date_by = ucwords($date_by);

        for ($i=$start_num;$i<=$end_num;$i++) {
            $labels[] = $i.$unit;
            $li = ($i<10) ? '0'.$i : $i;
            $default_format = ($date_by != 'Hour') ? $date_time.'-'.$li : $date_time.' '.$li;

            $tmp_show_data = array();
            $tmp_show_data[] = (isset($data['register'][$default_format])) ? ($data['register'][$default_format]) : 0;
            $tmp_show_data[] =  (isset($data['confirm'][$default_format])) ? ($data['confirm'][$default_format]) : 0;
            $tmp_show_data[] = (isset($data['active'][$default_format])) ? ($data['active'][$default_format]) : 0;
            $tmp_show_data[] = (isset($data['expired'][$default_format]) && isset($data['expired_confirm'][$default_format])) ? ($data['expired'][$default_format]-$data['expired_confirm'][$default_format]) : 0;
            $tmp_show_data[] = (isset($data['expired_confirm'][$default_format])) ? ($data['expired_confirm'][$default_format]) : 0; 
            $show_data[] = $tmp_show_data;
        }
        
        //json encode.
        $picture_title = AC::MEMBERSHIP_REG_ACTIVE_CONFIRM_TITLE;
        if (count($labels) <=15 ) {
            $labels_string = json_encode($labels);
            $data_string = json_encode($show_data);
            $body_list = "<center>".
                         "<input type='hidden' class='columns' labels='{$labels_string}' captures='{$captures}' datas='{$data_string}' title='{$picture_title} By {$date_by}' />".
                         "</center>";
        }
        else {
            $body_list = "<center>";
            $count = count($labels);
            $begin_num = intval($count/2);
            $second_num = ($date_by != 'Hour') ? $count-$begin_num-1 : $count-$begin_num;
            $labels_string_1 = json_encode(array_slice($labels,0,$begin_num));
            $labels_string_2 = json_encode(array_slice($labels,$second_num));

            $data_string_1 = json_encode(array_slice($show_data,0,$begin_num));
            $data_string_2 = json_encode(array_slice($show_data,$second_num));

            $part_I = ($date_by != 'Hour') ? "1$unit~".$begin_num."$unit" : "00$unit~".($begin_num-1)."$unit";
            $part_II = ($date_by != 'Hour') ? ($begin_num+1)."$unit~".$count."$unit" : $begin_num."$unit~".($count-1)."$unit";

            $body_list .= "<input type='hidden' class='columns' labels='{$labels_string_1}' captures='{$captures}' datas='{$data_string_1}' title='{$picture_title} By {$date_by} ({$part_I})' />";
            $body_list .= "<input type='hidden' class='columns' labels='{$labels_string_2}' captures='{$captures}' datas='{$data_string_2}' title='{$picture_title} By {$date_by} ({$part_II})' />";
            $body_list .= "</center>";
        }

        return $body_list;

    }

    private function getBodyDataList($condition) {
        $date_by = $condition['date_by'];
        $date_time = $condition['date_time'];
        $obj = new ModelReportPages();
        $data = $obj->getRegActiveConfirmData($date_by,$date_time);
        return $data;
    }
   
    private function setMstatInlineRegActiveConfirmBodyString($condition) {
        $data = $this->getBodyDataList($condition);
        $body_list = null;
        $body_list .= $this->setBodyCharString($condition['date_time'],$condition['date_by'],$data);
        $body_list .= '<br/>'.
                      '<table width="100%">';
        $body_list .= $this->setHeaderString($condition['date_by']);
        $body_list .= $this->setBodyString($condition['date_time'],$condition['date_by'],$data);
        $body_list .= '</table>';
        return $body_list;
    }


    //entrance.
    protected function __renderHead() {
        $body_string = null;
        $rand_code = rand(10000,99999);

        //parameters re-assignment.
        $conditions = $_GET;
        $conditions = $this->getPageCondition($conditions);

        //get the header string.
        $head_string = $this->setHeadString($conditions['condition'],null,$rand_code);
        
        //body string.
        $body_string .= $this->setMstatInlineRegActiveConfirmBodyString($conditions['condition']);

        //write a tmp file Export Tool.
        $export_html_string = $this->getOutPutBody($head_string['export'],$body_string);
        $write_result = AC::getInstance()->writeTempBodyToFile($rand_code,$export_html_string);
        
        //output.
        $this->contents =$this->getOutPutBody($head_string['view'],$body_string);
        
    }

}

?>